package Classes.GameViews.ViewImgs// ActionScript file
{
	import Classes.GameViews.GameView;
	import Classes.Managers.M_LoadPic;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.ByteArray;

	/**
	 *载入容器类 
	 * @author upstream
	 * 
	 */	
	public class ViewImg extends GameView
	{
		private var _imgDisplayObj:DisplayObject/*显示对象*/
		public function ViewImg():void
		{
			
		}
		
		/**
		 *创建一个图片或动画的载入容器 
		 * @param type
		 * @param id
		 * @param signID
		 * @return 
		 * 
		 */		
		public static function creat(type:String,id:int,signID:int):ViewImg
		{
			var returnTarget:ViewImg=new ViewImg;
			var getByteAry:ByteArray;
			getByteAry=M_LoadPic.instance.getSWFDataByName(type+"_"+id+"_"+signID,returnTarget);
			if(!getByteAry)getByteAry=M_LoadPic.instance.getSWFDataByName(type+"_0000_0",returnTarget);
			if(!getByteAry)throw new Error("找不到该种图片或动画资源");
			returnTarget.getLoadByteAry(getByteAry);
			return returnTarget;
		}
		
		/**
		 *获得显示对象的二进制数据并以此开始创建loader对象 
		 * @param byteAry 二进制数据数组
		 * 
		 */		
		public function getLoadByteAry(byteAry:ByteArray):void
		{
			var loader:Loader=new Loader;
			loader.loadBytes(byteAry);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCreatOver);
		}
		
		/**
		 *根据二进制创建的loader对象创建完成 
		 * @param e
		 * 
		 */		
		private function loaderCreatOver(e:Event):void
		{
			(e.target as LoaderInfo).removeEventListener(Event.COMPLETE,loaderCreatOver);
			this.setDisplayObj((e.target as LoaderInfo).loader);
			this.dispatchEvent(new Event("displayLoadOver"));
		}
		
		/**
		 *设定容器内的显示对象 
		 * @param dp 显示对象
		 * 
		 */		
		public function setImgDisplayObj(dp:DisplayObject):void
		{
			if(this.imgDisplayObj)this.removeChild(this.imgDisplayObj);
			this.imgDisplayObj=dp;
			this.addChild(this.imgDisplayObj);
		}
		
		/**
		 *释放 
		 * 
		 */		
		public function release():void
		{
			if(this.parent)this.parent.removeChild(this);
			if(this.displayObj)this.removeChild(this.displayObj);
			this.displayObj=null;
		}

		/*getter & setter*/
		public function get imgDisplayObj():DisplayObject
		{
			return _imgDisplayObj;
		}

		public function set imgDisplayObj(value:DisplayObject):void
		{
			_imgDisplayObj = value;
		}
	}
}